/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.outpatient.service.impl;
import me.zhengjie.modules.his.domain.Report;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.outpatient.repository.ReportRepository;
import me.zhengjie.modules.his.outpatient.service.ReportService;
import me.zhengjie.modules.his.outpatient.service.dto.ReportDto;
import me.zhengjie.modules.his.outpatient.service.dto.ReportQueryCriteria;
import me.zhengjie.modules.his.outpatient.service.mapstruct.ReportMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Liu
* @date 2022-11-05
**/
@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final ReportRepository reportRepository;
    private final ReportMapper reportMapper;

    @Override
    public Map<String,Object> queryAll(ReportQueryCriteria criteria, Pageable pageable){
        Page<Report> page = reportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reportMapper::toDto));
    }

    @Override
    public List<ReportDto> queryAll(ReportQueryCriteria criteria){
        return reportMapper.toDto(reportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReportDto findById(Integer reportId) {
        Report report = reportRepository.findById(reportId).orElseGet(Report::new);
        ValidationUtil.isNull(report.getReportId(),"Report","reportId",reportId);
        return reportMapper.toDto(report);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportDto create(Report resources) {
        return reportMapper.toDto(reportRepository.save(resources));
    }
    @Transactional(rollbackFor = Exception.class)
    public ReportDto modifyPathogeny(String pathogeny) {
        Report report = reportRepository.findById(1).orElseGet(Report::new);//先拿到
        report.setPathogeny(pathogeny);//在填写病因
        return reportMapper.toDto(reportRepository.save(report));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Report resources) {
        Report report = reportRepository.findById(resources.getReportId()).orElseGet(Report::new);
        ValidationUtil.isNull( report.getReportId(),"Report","id",resources.getReportId());
        report.copy(resources);
        reportRepository.save(report);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer reportId : ids) {
            reportRepository.deleteById(reportId);
        }
    }

    @Override
    public void download(List<ReportDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReportDto report : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", report.getReportName());
            map.put("性别", report.getSex());
            map.put("年龄", report.getAge());
            map.put("科室", report.getDepartment());
            map.put("医生", report.getDoctor());
            map.put("挂号类别", report.getReportType());
            map.put("挂号费", report.getPrice());
            map.put("时间", report.getTime());
            map.put("操作员", report.getUsers());
            map.put("状态", report.getState());
            map.put("手机号", report.getPhone());
            map.put(" carId",  report.getCarId());
            map.put("病因", report.getPathogeny());
            map.put("创建者", report.getCreateBy());
            map.put("更新者", report.getUpdateBy());
            map.put("创建日期", report.getCreateTime());
            map.put("更新时间", report.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
